//
//  MyTicketVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/4/22.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "MyTicketVC.h"

@interface MyTicketVC () {
    int pIntPage;
    NSString *strType;
}

@property(nonatomic, strong) NSMutableArray *pDataDicArr;

@property (weak, nonatomic) IBOutlet UILabel *lblMessage;

@end

@implementation MyTicketVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)initInfo
{
    [super initInfo];
    strType = @"0";
    [self selAddLeftBackButton];
    _pDataDicArr = [NSMutableArray array];
    [self selDefaultNavigationWithTitle:@"我的电影票"];
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1.0f];
    
    __weak typeof(self) weakSelf = self;
    self.pTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pIntPage = 1;
        [weakSelf requestData];
    }];
    
    self.pTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestData];
    }];
    
    [self.pTableView.mj_header beginRefreshing];
    [self.pTableView.mj_footer setHidden:YES];
}

- (IBAction)btnSelectTypeAction:(UIButton *)sender
{
    UIButton *btn1 = (UIButton *)[self.view viewWithTag:1300];
    UIButton *btn2 = (UIButton *)[self.view viewWithTag:1301];
    UIButton *btn3 = (UIButton *)[self.view viewWithTag:1302];
    [btn1 setSelected:NO];
    [btn2 setSelected:NO];
    [btn3 setSelected:NO];
    sender.selected = YES;
    
    strType = [NSString stringWithFormat:@"%d", (int)sender.tag - 1300];
    pIntPage = 1;
    [self.pTableView.mj_header beginRefreshing];
}

-(void)requestData
{
    __weak typeof(self) weakSelf = self;
    [TDNetwork requestGetPrizesListPage:pIntPage andPageSize:10 status:strType complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary)
     {
         if (succeed) {
             if (pIntPage == 1) {
                 [_pDataDicArr removeAllObjects];
             }
             NSArray *array = [dictionary valueForKey:@"res"];
             if (array && [array isKindOfClass:[NSArray class]]) {
                 [weakSelf.pDataDicArr addObjectsFromArray:array];
                 
                 if (array.count == 10) {
                     pIntPage ++;
                     [self.pTableView.mj_footer setHidden:NO];
                 } else {
                     [self.pTableView.mj_footer setHidden:YES];
                 }
             }
             [weakSelf.pTableView reloadData];
             
         } else {
         }
         
         [self.pTableView.mj_footer endRefreshing];
         [self.pTableView.mj_header endRefreshing];
         
         if (_pDataDicArr.count == 0) {
             [_lblMessage setHidden:NO];
         } else {
             [_lblMessage setHidden:YES];
         }
     }];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _pDataDicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idfMyTicketCell1"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSDictionary *dict = [_pDataDicArr objectAtIndex:indexPath.row];
    NSDictionary *dictInfo = [dict objectForKey:[strType isEqualToString:@"1"]?@"prize":@"active"];
    UILabel *lblTicketNum = (UILabel *)[cell.contentView viewWithTag:1101];
    UILabel *lblTicketMovieTitle = (UILabel *)[cell.contentView viewWithTag:1102];
    UILabel *lblTicketMovieDescribe = (UILabel *)[cell.contentView viewWithTag:1103];
    UILabel *lblTicketMovieDate = (UILabel *)[cell.contentView viewWithTag:1104];
    UIButton *btnGet = (UIButton *)[cell.contentView viewWithTag:1301];
    
    [lblTicketNum setText:@"1张"];
    [lblTicketMovieTitle setText:[dictInfo objectForKey:@"name"]];
    [lblTicketMovieDescribe setText:[dictInfo objectForKey:@"des"]];
    
    if ([strType isEqualToString:@"0"]) {
        NSString *strStartTime = [dictInfo objectForKey:@"starttime"];
        @try {
            strStartTime = [strStartTime substringToIndex:10];
            strStartTime = [strStartTime stringByReplacingOccurrencesOfString:@"-" withString:@"."];
        } @catch (NSException *exception) {
        } @finally {
        }
        NSString *strEndTime = [dictInfo objectForKey:@"endtime"];
        @try {
            strEndTime = [strEndTime substringToIndex:10];
            strEndTime = [strEndTime stringByReplacingOccurrencesOfString:@"-" withString:@"."];
        } @catch (NSException *exception) {
        } @finally {
        }
        [lblTicketMovieDate setText:[NSString stringWithFormat:@"%@-%@", strStartTime, strEndTime]];
    } else if ([strType isEqualToString:@"1"]) {
        NSDictionary *dictActive = [dict objectForKey:@"active"];
        NSString *strTimeOut = [dictActive objectForKey:@"endtime"];
        @try {
            strTimeOut = [strTimeOut substringToIndex:10];
            strTimeOut = [strTimeOut stringByReplacingOccurrencesOfString:@"-" withString:@"."];
        } @catch (NSException *exception) {
        } @finally {
        }
        [lblTicketMovieDate setText:[NSString stringWithFormat:@"%@", strTimeOut]];
    } else {
        [lblTicketMovieDate setText:@"已过期"];
    }
    
    if ([strType isEqualToString:@"0"]) {
        [btnGet setHidden:NO];
        [btnGet setAccessibilityIdentifier:[dict objectForKey:@"id"]];
        [btnGet addTarget:self action:@selector(btnGetTicketAction:) forControlEvents:UIControlEventTouchUpInside];
    } else {
        [btnGet setHidden:YES];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ([UIScreen mainScreen].bounds.size.width - 30) * 24 / 69 + 15;
}

- (void)btnGetTicketAction:(UIButton *)sender
{
    [SVProgressHUD show];
    [TDNetwork requestReceivePrize:sender.accessibilityIdentifier complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [SVProgressHUD showSuccessWithStatus:@"领取成功"];
            [self.pTableView.mj_header beginRefreshing];
        } else {
            [SVProgressHUD showErrorWithStatus:@"领取失败,请重试!"];
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
